package org.example.captchademo.Controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import cn.hutool.captcha.LineCaptcha;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RequestMapping("/captcha1")
@RestController
public class CaptchaController_demo {
//    @Autowired
//    private
    @RequestMapping("/getCaptcha1")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response) {
//      定义验证码长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
//      图形验证码写出，可以写出到文件，也可以写出到流
        lineCaptcha.write("d:/line.png");
//      输出code
        System.out.println(lineCaptcha.getCode());
        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
//        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
//        captcha.write("d:/circle.png");

        ICaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);
        try {
            captcha.write(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
